<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深入理解 Java volatile 关键字</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }
        
        .code-block::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 40px;
            background: #2d2d2d;
            border-bottom: 1px solid #3e3e3e;
        }
        
        .code-dots {
            position: absolute;
            top: 12px;
            left: 16px;
            display: flex;
            gap: 8px;
        }
        
        .code-dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            margin-top: -0.2rem;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        
        .section-number {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            width: 36px;
            height: 36px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 50%;
            font-weight: 700;
            margin-right: 12px;
            flex-shrink: 0;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 1.5rem;
            border-radius: 16px;
            position: relative;
            overflow: hidden;
        }
        
        .highlight-box::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
            animation: pulse 3s ease-in-out infinite;
        }
        
        @keyframes pulse {
            0%, 100% { transform: scale(1); opacity: 0.5; }
            50% { transform: scale(1.1); opacity: 0.8; }
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 24px;
            margin-bottom: 1rem;
        }
        
        .gradient-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center">
            <h1 class="text-5xl md:text-7xl font-bold mb-6 animate-pulse">
                深入理解 Java <span class="inline-block transform rotate-3">volatile</span> 关键字
            </h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto leading-relaxed">
                <i class="fas fa-shield-alt mr-2"></i>
                掌握多线程编程的核心机制，确保并发环境下的数据一致性
            </p>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-6xl mx-auto px-6 py-12">
        <!-- Introduction Card -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">v</span>olatile 关键字在Java中通过内存屏障禁止指令重排，并确保变量的可见性。这对于确保在多线程环境中的正确性至关重要。然而，<code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">volatile</code> 仅保证可见性和禁止重排，不提供原子性和互斥性，因此在需要更复杂的同步控制时，还需配合其他同步机制使用。
            </p>
        </div>

        <!-- Section 1: Basic Functions -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="text-3xl font-bold mb-6 flex items-center">
                <span class="section-number">1</span>
                <code class="gradient-text">volatile</code> 关键字的基本作用
            </h2>
            
            <div class="grid md:grid-cols-2 gap-6">
                <div class="bg-gradient-to-br from-purple-50 to-pink-50 p-6 rounded-xl">
                    <div class="feature-icon">
                        <i class="fas fa-eye"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">可见性</h3>
                    <p class="text-gray-700">当一个线程修改了 <code class="bg-white px-2 py-1 rounded text-purple-600">volatile</code> 变量的值，其他线程能够立即看到这个修改。确保了变量的更新对所有线程都是立即可见的。</p>
                </div>
                
                <div class="bg-gradient-to-br from-blue-50 to-indigo-50 p-6 rounded-xl">
                    <div class="feature-icon" style="background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);">
                        <i class="fas fa-ban"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">禁止指令重排</h3>
                    <p class="text-gray-700"><code class="bg-white px-2 py-1 rounded text-blue-600">volatile</code> 可以防止指令重排问题，确保变量的读写操作不会与其他操作重排，保证程序的执行顺序符合程序员的预期。</p>
                </div>
            </div>
        </div>

        <!-- Section 2: Instruction Reordering -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="text-3xl font-bold mb-6 flex items-center">
                <span class="section-number">2</span>
                指令重排的概念
            </h2>
            <p class="text-lg leading-relaxed text-gray-700 indent-8">
                指令重排是指编译器和处理器在执行代码时为了提高性能，可能会改变指令的执行顺序。重排通常不会改变程序的最终结果，但可能会在多线程环境中引入潜在的同步问题。
            </p>
            
            <!-- Visualization -->
            <div class="mt-8">
                <div class="mermaid">
                    graph LR
                        A[原始代码顺序] --> B[编译器优化]
                        B --> C[处理器重排]
                        C --> D[实际执行顺序]
                        style A fill:#667eea,stroke:#fff,color:#fff
                        style D fill:#764ba2,stroke:#fff,color:#fff
                </div>
            </div>
        </div>

        <!-- Section 3: volatile and Reordering -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="text-3xl font-bold mb-6 flex items-center">
                <span class="section-number">3</span>
                <code class="gradient-text">volatile</code> 与指令重排
            </h2>
            
            <div class="space-y-6">
                <div class="border-l-4 border-purple-500 pl-6">
                    <h3 class="text-xl font-bold mb-2 text-purple-700">
                        <i class="fas fa-shield-alt mr-2"></i>禁止重排的语义
                    </h3>
                    <p class="text-gray-700">在 <code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">volatile</code> 变量的读写操作之前和之后，JVM会插入适当的内存屏障（memory barrier），以防止对 <code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">volatile</code> 变量的读写操作被重排。</p>
                </div>
                
                <div class="border-l-4 border-blue-500 pl-6">
                    <h3 class="text-xl font-bold mb-2 text-blue-700">
                        <i class="fas fa-book-reader mr-2"></i>读取 <code>volatile</code> 变量
                    </h3>
                    <p class="text-gray-700">当读取一个 <code class="bg-blue-100 text-blue-700 px-2 py-1 rounded">volatile</code> 变量时，JVM会强制刷新CPU缓存中该变量的值，从主内存中重新读取数据。</p>
                </div>
                
                <div class="border-l-4 border-green-500 pl-6">
                    <h3 class="text-xl font-bold mb-2 text-green-700">
                        <i class="fas fa-pen mr-2"></i>写入 <code>volatile</code> 变量
                    </h3>
                    <p class="text-gray-700">当写入一个 <code class="bg-green-100 text-green-700 px-2 py-1 rounded">volatile</code> 变量时，JVM会将该变量的值立即写入主内存，并且在写操作之前会确保所有之前的写操作都已经完成。</p>
                </div>
            </div>
        </div>

        <!-- Section 4: Example -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="text-3xl font-bold mb-6 flex items-center">
                <span class="section-number">4</span>
                示例与解释
            </h2>
            
            <div class="code-block p-8 pt-16 relative">
                <div class="code-dots">
                    <div class="code-dot bg-red-500"></div>
                    <div class="code-dot bg-yellow-500"></div>
                    <div class="code-dot bg-green-500"></div>
                </div>
                <pre class="text-sm md:text-base"><code><span class="text-purple-400">public class</span> <span class="text-yellow-300">Demo</span> {
    <span class="text-purple-400">private volatile boolean</span> <span class="text-blue-300">flag</span> = <span class="text-orange-300">false</span>;
    <span class="text-purple-400">private int</span> <span class="text-blue-300">count</span> = <span class="text-orange-300">0</span>;

    <span class="text-purple-400">public void</span> <span class="text-yellow-300">thread1</span>() {
        <span class="text-blue-300">count</span> = <span class="text-orange-300">1</span>;
        <span class="text-blue-300">flag</span> = <span class="text-orange-300">true</span>;
    }

    <span class="text-purple-400">public void</span> <span class="text-yellow-300">thread2</span>() {
        <span class="text-purple-400">if</span> (<span class="text-blue-300">flag</span>) {
            <span class="text-gray-500">// 这里 count 必定是 1</span>
            <span class="text-yellow-300">System</span>.<span class="text-blue-300">out</span>.<span class="text-yellow-300">println</span>(<span class="text-blue-300">count</span>);
        }
    }
}</code></pre>
            </div>
            
            <div class="mt-6 p-6 bg-gradient-to-r from-purple-50 to-pink-50 rounded-xl">
                <p class="text-gray-700 leading-relaxed indent-8">
                    在